import { handleActions } from "redux-actions";
import { ADD_COMMODITY, DEL_COMMODITY, GET_COMMODITY, FIND_COMMODITY, EDIT_COMMODITY, COMMODITY_SET_CUR_PAGE, COMMODITY_SET_EACH_PAGE } from "../types";
import _ from "lodash";


const initData = {
    curPage: 1,
    eachPage: 5,
    total: 0,
    maxPage: 0,
    rows: [],
}

export const commodity = handleActions({
    //获取商品数据
    [GET_COMMODITY]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        // 合并数据
        return _.assign({}, state, {
            total, maxPage, rows,
        })
    },

    // 新增商品数据
    [ADD_COMMODITY]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        // 合并数据
        return _.assign({}, state, {
            total, maxPage, rows
        })
    },

    // 删除商品数据
    [DEL_COMMODITY]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        // 合并数据
        return _.assign({}, state, {
            total, maxPage, rows
        })
    },

    // 名称查找商品
    [FIND_COMMODITY]: (state, { payload }) => {
        const { rows } = payload;

        // console.log(rows);
        return _.assign({}, state, {
            rows
        })
    },

    // 修改商品数据
    [EDIT_COMMODITY]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        // 合并数据
        return _.assign({}, state, {
            total, maxPage, rows
        })
    },

    // 设置商品当前页
    [COMMODITY_SET_CUR_PAGE]: (state, { payload }) => {
        const { curPage } = payload;
        const $state = _.clone(state);
        $state.curPage = curPage;
        return $state;
    },

    // 设置商品每页显示条数
    [COMMODITY_SET_EACH_PAGE]: (state, { payload }) => {
        const { eachPage } = payload;
        const $state = _.clone(state);
        $state.eachPage = eachPage;
        // $state.rows = [];
        return $state;
    },

}, initData)